﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing

Namespace DB

    ''' <summary>
    ''' TypeName:    StockItemType
    ''' Author:      GeneratedCustomTemplate
    ''' DBTableName: STK_Stock
    ''' </summary>
    ''' <remarks></remarks>

    Public Class StockItemType
        Inherits DBTableObjectBase

#Region "Declarations"
        'Protected Friend iStockItemId as Guid
        Protected Friend iItemCode As String
        Protected Friend iDescription As String
        Protected Friend iCategory As String
        Protected Friend iSubCategory As String
        Protected Friend iModel As String
        Protected Friend iBrand As String
        Protected Friend iColor As String
        Protected Friend iWeight As String
        Protected Friend iVolume As String
        Protected Friend iManufacturer As String
        Protected Friend iUnit As String
        Protected Friend iQuantity As Decimal
        Protected Friend iCostPrice As Decimal
        Protected Friend iInstallationCharge As Decimal
        Protected Friend iSellingPrice As Decimal
        Protected Friend iTaxPercentage As Decimal
        Protected Friend iHasSerial As String = "N"
        Protected Friend iCreatedDate As DateTime
#End Region

#Region "Constructors"
        Public Sub New()
            MyBase.New(TableNamesEnum.STK_Stock)
        End Sub
        Public Sub New(ByVal StockItemId As Guid)
            MyBase.New(TableNamesEnum.STK_Stock, StockItemId)
        End Sub
        Public Sub New(ByVal StockItemId As Guid, _
                        ByVal ItemCode As String, _
                        ByVal Description As String, _
                        ByVal Category As String, _
                        ByVal SubCategory As String, _
                        ByVal Model As String, _
                        ByVal Brand As String, _
                        ByVal Color As String, _
                        ByVal Weight As String, _
                        ByVal Volume As String, _
                        ByVal Manufacturer As String, _
                        ByVal Unit As String, _
                        ByVal Quantity As Decimal, _
                        ByVal CostPrice As Decimal, _
                        ByVal SellingPrice As Decimal, _
                        ByVal TaxPercentage As Decimal, _
                        ByVal HasSerial As String, _
                        ByVal CreatedDate As DateTime, _
                        ByVal LMUserId As Guid, _
                        ByVal LMDate As DateTime)
            MyBase.New(TableNamesEnum.STK_Stock)
            Me.iId = StockItemId
            Me.iItemCode = ItemCode
            Me.iDescription = Description
            Me.iCategory = Category
            Me.iSubCategory = SubCategory
            Me.iModel = Model
            Me.iBrand = Brand
            Me.iColor = Color
            Me.iWeight = Weight
            Me.iVolume = Volume
            Me.iManufacturer = Manufacturer
            Me.iUnit = Unit
            Me.iQuantity = Quantity
            Me.iCostPrice = CostPrice
            Me.iSellingPrice = SellingPrice
            Me.iTaxPercentage = TaxPercentage
            Me.iHasSerial = HasSerial
            Me.iCreatedDate = CreatedDate
            Me.iLMUserId = LMUserId
            Me.iLMDate = LMDate
        End Sub
        Public Sub New(ByVal StockItem As StockItemType)
            MyBase.New(TableNamesEnum.STK_Stock)
            Me.iId = StockItem.StockItemId
            Me.iItemCode = StockItem.ItemCode
            Me.iDescription = StockItem.Description
            Me.iCategory = StockItem.Category
            Me.iSubCategory = StockItem.SubCategory
            Me.iModel = StockItem.Model
            Me.iBrand = StockItem.Brand
            Me.iColor = StockItem.Color
            Me.iWeight = StockItem.Weight
            Me.iVolume = StockItem.Volume
            Me.iManufacturer = StockItem.Manufacturer
            Me.iUnit = StockItem.Unit
            Me.iQuantity = StockItem.Quantity
            Me.iCostPrice = StockItem.CostPrice
            Me.iSellingPrice = StockItem.SellingPrice
            Me.iTaxPercentage = StockItem.TaxPercentage
            Me.iHasSerial = StockItem.HasSerial
            Me.iCreatedDate = StockItem.CreatedDate
            Me.iLMUserId = StockItem.LMUserId
            Me.iLMDate = StockItem.LMDate
        End Sub
#End Region

#Region "Exposed Properties"
        Public Property StockItemId() As Guid
            Get
                Return Me.iId
            End Get
            Set(ByVal value As Guid)
                Me.iId = value
            End Set
        End Property
        Public Property ItemCode() As String
            Get
                Return Me.iItemCode
            End Get
            Set(ByVal value As String)
                Me.iItemCode = value
            End Set
        End Property
        Public Property Description() As String
            Get
                Return Me.iDescription
            End Get
            Set(ByVal value As String)
                Me.iDescription = value
            End Set
        End Property
        Public Property Category() As String
            Get
                Return Me.iCategory
            End Get
            Set(ByVal value As String)
                Me.iCategory = value
            End Set
        End Property
        Public Property SubCategory() As String
            Get
                Return Me.iSubCategory
            End Get
            Set(ByVal value As String)
                Me.iSubCategory = value
            End Set
        End Property
        Public Property Model() As String
            Get
                Return Me.iModel
            End Get
            Set(ByVal value As String)
                Me.iModel = value
            End Set
        End Property
        Public Property Brand() As String
            Get
                Return Me.iBrand
            End Get
            Set(ByVal value As String)
                Me.iBrand = value
            End Set
        End Property
        Public Property Color() As String
            Get
                Return Me.iColor
            End Get
            Set(ByVal value As String)
                Me.iColor = value
            End Set
        End Property
        Public Property Weight() As String
            Get
                Return Me.iWeight
            End Get
            Set(ByVal value As String)
                Me.iWeight = value
            End Set
        End Property
        Public Property Volume() As String
            Get
                Return Me.iVolume
            End Get
            Set(ByVal value As String)
                Me.iVolume = value
            End Set
        End Property
        Public Property Manufacturer() As String
            Get
                Return Me.iManufacturer
            End Get
            Set(ByVal value As String)
                Me.iManufacturer = value
            End Set
        End Property
        Public Property Unit() As String
            Get
                Return Me.iUnit
            End Get
            Set(ByVal value As String)
                Me.iUnit = value
            End Set
        End Property
        Public Property Quantity() As Decimal
            Get
                Return Me.iQuantity
            End Get
            Set(ByVal value As Decimal)
                Me.iQuantity = value
            End Set
        End Property
        Public Property CostPrice() As Decimal
            Get
                Return Me.iCostPrice
            End Get
            Set(ByVal value As Decimal)
                Me.iCostPrice = value
            End Set
        End Property
        Public Property InstallationCharge As Decimal
            Get
                Return Me.iInstallationCharge
            End Get
            Set(value As Decimal)
                Me.iInstallationCharge = value
            End Set
        End Property
        Public Property SellingPrice() As Decimal
            Get
                Return Me.iSellingPrice
            End Get
            Set(ByVal value As Decimal)
                Me.iSellingPrice = value
            End Set
        End Property
        Public Property TaxPercentage() As Decimal
            Get
                Return Me.iTaxPercentage
            End Get
            Set(ByVal value As Decimal)
                Me.iTaxPercentage = value
            End Set
        End Property
        Public Property HasSerial() As Boolean
            Get
                If Me.iHasSerial = "Y" Then
                    Return True
                Else
                    Return False
                End If
            End Get
            Set(ByVal value As Boolean)
                If value Then
                    Me.iHasSerial = "Y"
                Else
                    Me.iHasSerial = "N"
                End If
            End Set
        End Property
        Public Property CreatedDate As DateTime
            Get
                Return Me.iCreatedDate
            End Get
            Set(value As DateTime)
                Me.iCreatedDate = value
            End Set
        End Property
#End Region

#Region "Implementations"
        Public Overloads Function Load(ByVal Row As DataRow, Optional ColumnOffset As Integer = 0) As Boolean
            Dim colIndex As Integer = ColumnOffset
            If IsDBNull(Row(colIndex + 0)) Then Me.iId = Guid.Empty Else Me.iId = Row(colIndex + 0)
            If IsDBNull(Row(colIndex + 1)) Then Me.iItemCode = Nothing Else Me.iItemCode = Row(colIndex + 1)
            If IsDBNull(Row(colIndex + 2)) Then Me.iDescription = Nothing Else Me.iDescription = Row(colIndex + 2)
            If IsDBNull(Row(colIndex + 3)) Then Me.iCategory = Nothing Else Me.iCategory = Row(colIndex + 3)
            If IsDBNull(Row(colIndex + 4)) Then Me.iSubCategory = Nothing Else Me.iSubCategory = Row(colIndex + 4)
            If IsDBNull(Row(colIndex + 5)) Then Me.iModel = Nothing Else Me.iModel = Row(colIndex + 5)
            If IsDBNull(Row(colIndex + 6)) Then Me.iBrand = Nothing Else Me.iBrand = Row(colIndex + 6)
            If IsDBNull(Row(colIndex + 7)) Then Me.iColor = Nothing Else Me.iColor = Row(colIndex + 7)
            If IsDBNull(Row(colIndex + 8)) Then Me.iWeight = Nothing Else Me.iWeight = Row(colIndex + 8)
            If IsDBNull(Row(colIndex + 9)) Then Me.iVolume = Nothing Else Me.iVolume = Row(colIndex + 9)
            If IsDBNull(Row(colIndex + 10)) Then Me.iManufacturer = Nothing Else Me.iManufacturer = Row(colIndex + 10)
            If IsDBNull(Row(colIndex + 11)) Then Me.iUnit = Nothing Else Me.iUnit = Row(colIndex + 11)
            If IsDBNull(Row(colIndex + 12)) Then Me.iQuantity = Nothing Else Me.iQuantity = Row(colIndex + 12)
            If IsDBNull(Row(colIndex + 13)) Then Me.iCostPrice = Nothing Else Me.iCostPrice = Row(colIndex + 13)
            If IsDBNull(Row(colIndex + 14)) Then Me.iInstallationCharge = Nothing Else Me.iInstallationCharge = Row(colIndex + 14)
            If IsDBNull(Row(colIndex + 15)) Then Me.iSellingPrice = Nothing Else Me.iSellingPrice = Row(colIndex + 15)
            If IsDBNull(Row(colIndex + 16)) Then Me.iTaxPercentage = Nothing Else Me.iTaxPercentage = Row(colIndex + 16)
            If IsDBNull(Row(colIndex + 17)) Then Me.iHasSerial = Nothing Else Me.iHasSerial = Row(colIndex + 17)
            If IsDBNull(Row(colIndex + 18)) Then Me.iCreatedDate = Nothing Else Me.iCreatedDate = Row(colIndex + 18)
            If IsDBNull(Row(colIndex + 19)) Then Me.iLMUserId = Nothing Else Me.iLMUserId = Row(colIndex + 19)
            If IsDBNull(Row(colIndex + 20)) Then Me.iLMDate = Nothing Else Me.iLMDate = Row(colIndex + 20)
            Return True
        End Function
        Protected Friend Overrides Function Load() As Boolean
            'sp_[TableName]Get
            Dim resBool As Boolean = True
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Get '" & Me.iId.ToString("D") & "'")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction

            Dim dt As DataTable = New DataTable
            dt = Utility.GetList(cmd)

            If dt.Rows.Count > 0 Then
                Me.Load(dt.Rows(0))
            Else
                resBool = False
            End If

            Return resBool
        End Function
        Protected Friend Overrides Function Insert() As Boolean
            'sp_[TableName]Insert
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter

            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Insert")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If (Me.iId = Guid.Empty) Then
                        .AddWithValue("@StockItemId", System.DBNull.Value)
                    Else
                        .AddWithValue("@StockItemId", Me.iId)
                    End If
                    If (Me.iItemCode = String.Empty) Then
                        .AddWithValue("@ItemCode", System.DBNull.Value)
                    Else
                        .AddWithValue("@ItemCode", Me.iItemCode)
                    End If
                    If (Me.iDescription = String.Empty) Then
                        .AddWithValue("@Description", System.DBNull.Value)
                    Else
                        .AddWithValue("@Description", Me.iDescription)
                    End If
                    If (Me.iCategory = String.Empty) Then
                        .AddWithValue("@Category", System.DBNull.Value)
                    Else
                        .AddWithValue("@Category", Me.iCategory)
                    End If
                    If (Me.iSubCategory = String.Empty) Then
                        .AddWithValue("@SubCategory", System.DBNull.Value)
                    Else
                        .AddWithValue("@SubCategory", Me.iSubCategory)
                    End If
                    If (Me.iModel = String.Empty) Then
                        .AddWithValue("@Model", System.DBNull.Value)
                    Else
                        .AddWithValue("@Model", Me.iModel)
                    End If
                    If (Me.iBrand = String.Empty) Then
                        .AddWithValue("@Brand", System.DBNull.Value)
                    Else
                        .AddWithValue("@Brand", Me.iBrand)
                    End If
                    If (Me.iColor = String.Empty) Then
                        .AddWithValue("@Color", System.DBNull.Value)
                    Else
                        .AddWithValue("@Color", Me.iColor)
                    End If
                    If (Me.iWeight = String.Empty) Then
                        .AddWithValue("@Weight", System.DBNull.Value)
                    Else
                        .AddWithValue("@Weight", Me.iWeight)
                    End If
                    If (Me.iVolume = String.Empty) Then
                        .AddWithValue("@Volume", System.DBNull.Value)
                    Else
                        .AddWithValue("@Volume", Me.iVolume)
                    End If
                    If (Me.iManufacturer = String.Empty) Then
                        .AddWithValue("@Manufacturer", System.DBNull.Value)
                    Else
                        .AddWithValue("@Manufacturer", Me.iManufacturer)
                    End If
                    If (Me.iUnit = String.Empty) Then
                        .AddWithValue("@Unit", System.DBNull.Value)
                    Else
                        .AddWithValue("@Unit", Me.iUnit)
                    End If
                    .AddWithValue("@Quantity", Me.iQuantity)
                    .AddWithValue("@CostPrice", Me.iCostPrice)
                    .AddWithValue("@InstallationCharge", Me.iInstallationCharge)
                    .AddWithValue("@SellingPrice", Me.iSellingPrice)
                    .AddWithValue("@TaxPercentage", Me.iTaxPercentage)
                    If (Me.iHasSerial = String.Empty) Then
                        .AddWithValue("@HasSerial", System.DBNull.Value)
                    Else
                        .AddWithValue("@HasSerial", Me.iHasSerial)
                    End If
                    If (Me.iCreatedDate = DateTime.MinValue) Then
                        .AddWithValue("@CreatedDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@CreatedDate", Me.iCreatedDate)
                    End If
                    If (Me.iLMUserId = Guid.Empty) Then
                        .AddWithValue("@LMUserId", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMUserId", Me.iLMUserId)
                    End If
                    If (Me.iLMDate = DateTime.MinValue) Then
                        .AddWithValue("@LMDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMDate", Me.iLMDate)
                    End If
                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw ex
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to insert, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function Update() As Boolean
            'sp_[TableName]Update
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Update")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If (Me.iId = Guid.Empty) Then
                        .AddWithValue("@StockItemId", System.DBNull.Value)
                    Else
                        .AddWithValue("@StockItemId", Me.iId)
                    End If
                    If (Me.iItemCode = String.Empty) Then
                        .AddWithValue("@ItemCode", System.DBNull.Value)
                    Else
                        .AddWithValue("@ItemCode", Me.iItemCode)
                    End If
                    If (Me.iDescription = String.Empty) Then
                        .AddWithValue("@Description", System.DBNull.Value)
                    Else
                        .AddWithValue("@Description", Me.iDescription)
                    End If
                    If (Me.iCategory = String.Empty) Then
                        .AddWithValue("@Category", System.DBNull.Value)
                    Else
                        .AddWithValue("@Category", Me.iCategory)
                    End If
                    If (Me.iSubCategory = String.Empty) Then
                        .AddWithValue("@SubCategory", System.DBNull.Value)
                    Else
                        .AddWithValue("@SubCategory", Me.iSubCategory)
                    End If
                    If (Me.iModel = String.Empty) Then
                        .AddWithValue("@Model", System.DBNull.Value)
                    Else
                        .AddWithValue("@Model", Me.iModel)
                    End If
                    If (Me.iBrand = String.Empty) Then
                        .AddWithValue("@Brand", System.DBNull.Value)
                    Else
                        .AddWithValue("@Brand", Me.iBrand)
                    End If
                    If (Me.iColor = String.Empty) Then
                        .AddWithValue("@Color", System.DBNull.Value)
                    Else
                        .AddWithValue("@Color", Me.iColor)
                    End If
                    If (Me.iWeight = String.Empty) Then
                        .AddWithValue("@Weight", System.DBNull.Value)
                    Else
                        .AddWithValue("@Weight", Me.iWeight)
                    End If
                    If (Me.iVolume = String.Empty) Then
                        .AddWithValue("@Volume", System.DBNull.Value)
                    Else
                        .AddWithValue("@Volume", Me.iVolume)
                    End If
                    If (Me.iManufacturer = String.Empty) Then
                        .AddWithValue("@Manufacturer", System.DBNull.Value)
                    Else
                        .AddWithValue("@Manufacturer", Me.iManufacturer)
                    End If
                    If (Me.iUnit = String.Empty) Then
                        .AddWithValue("@Unit", System.DBNull.Value)
                    Else
                        .AddWithValue("@Unit", Me.iUnit)
                    End If
                    .AddWithValue("@Quantity", Me.iQuantity)
                    .AddWithValue("@CostPrice", Me.iCostPrice)
                    .AddWithValue("@InstallationCharge", Me.iInstallationCharge)
                    .AddWithValue("@SellingPrice", Me.iSellingPrice)
                    .AddWithValue("@TaxPercentage", Me.iTaxPercentage)
                    If (Me.iHasSerial = String.Empty) Then
                        .AddWithValue("@HasSerial", System.DBNull.Value)
                    Else
                        .AddWithValue("@HasSerial", Me.iHasSerial)
                    End If
                    If (Me.iCreatedDate = DateTime.MinValue) Then
                        .AddWithValue("@CreatedDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@CreatedDate", Me.iCreatedDate)
                    End If
                    If (Me.iLMUserId = Guid.Empty) Then
                        .AddWithValue("@LMUserId", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMUserId", Me.iLMUserId)
                    End If
                    If (Me.iLMDate = DateTime.MinValue) Then
                        .AddWithValue("@LMDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMDate", Me.iLMDate)
                    End If
                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw ex
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to update, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function ValidateOnDelete() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnInsert() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnUpdate() As Boolean
            Return True
        End Function
#End Region

#Region "Overrides"
        Public Overrides Function ToString() As String
            Dim t As String = ""
            If Me.iItemCode <> "" Then
                t &= Me.iItemCode
            End If
            If Me.iDescription <> "" Then
                If t <> "" Then
                    t &= " - " & Me.iDescription
                Else
                    t &= Me.iDescription
                End If
            End If
            If Me.iCategory <> "" Then
                If Me.iSubCategory <> "" Then
                    t &= " (" & Me.iCategory & " , " & Me.iSubCategory & ")"
                Else
                    t &= " (" & Me.iCategory & ")"
                End If
            End If
            Return t
        End Function
        Public Overrides Function ToDetailsListBoxItem() As WindowsExtendedControls.DetailListBoxItem
            Dim header As String = ""
            Dim details As String = ""
            Dim footer As String = ""
            If Me.iItemCode <> "" And Me.iDescription <> "" Then
                header = Me.iItemCode & " - " & Me.iDescription
            ElseIf Me.iItemCode <> "" And Me.iDescription = "" Then
                header = Me.iItemCode
            Else
                Return Nothing
            End If
            Return New WindowsExtendedControls.DetailListBoxItem(header, details, footer, Nothing, Me)
        End Function
#End Region

    End Class

    Public Class StockItemTypeCollection
        Inherits DBTableObjectCollectionBase

        Public DataFilters As New DataFilterTypeCollection(Of StockItemTypeFields)(Me.iTablename.ToString)
        Public DataSorters As New DataSorterTypeCollection(Of StockItemTypeFields)(Me.iTableName.ToString)

        Public Sub New()
            MyBase.New(TableNamesEnum.STK_Stock)
        End Sub

        Default Public Property Item(ByVal Index As Integer) As StockItemType
            Get
                Return CType(Me.List.Item(Index), StockItemType)
            End Get
            Set(ByVal value As StockItemType)
                Me.List.Item(Index) = value
            End Set
        End Property

        Protected Friend Overrides Function Load() As Boolean
            Return Me.Load("SELECT * FROM " & Me.iTableName.ToString & Me.DataFilters.GetSQL & Me.DataSorters.GetSQL)
        End Function

        Protected Friend Overloads Function Load(ByVal CustomFilter As String) As Boolean
            'Get data from DB
            Dim cmd As New SqlCommand
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            Dim dt As DataTable
            Try
                cmd.CommandText = CustomFilter
                dt = Utility.GetList(cmd)
            Catch ex As Exception
                Throw ex
            End Try
            'Clear exising list
            Dim row As DataRow
            List.Clear()

            'Build new list
            Dim typeObject As StockItemType

            For Each row In dt.Rows
                typeObject = New StockItemType()
                typeObject.iConnection = Me.iConnection
                typeObject.iTransaction = Me.iTransaction
                If typeObject.Load(row) = True Then Me.List.Add(typeObject)
            Next
            Return True
        End Function

        Public Overloads Sub Sort(ByVal propertyToSort As StockItemTypeFields, ByVal sortOrder As SortOrder)
            ipropertyToSort = propertyToSort.ToString
            isortOrder = sortOrder
            Me.InnerList.Sort(Me)
        End Sub

    End Class

#Region "Enumerations"       'Enumerations
    Public Enum StockItemTypeFields
        StockItemId
        ItemCode
        Description
        Category
        SubCategory
        Model
        Brand
        Color
        Weight
        Volume
        Manufacturer
        Unit
        Quantity
        CostPrice
        SellingPrice
        TaxPercentage
        HasSerial
        CreatedDate
        LMUserId
        LMDate

    End Enum
#End Region

End Namespace